package com.qf.controller;

import com.qf.pojo.Type;
import com.qf.service.TypeService;

import com.qf.vo.MsgResult;
import com.qf.vo.TypeVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Controller
public class TypeController {

    @Resource
    private TypeService typeService ;

    @RequestMapping(value = "/load",method = RequestMethod.GET)
    public String all(Model model) {
        List<Type> types = typeService.getTypes1();
        model.addAttribute("types",types);
        return "index";
    }

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String first(Model model) {
        List<Type> types = typeService.getTypes1();
        model.addAttribute("types",types);
        return "index";
    }

    @ResponseBody
    @RequestMapping(value = "C2C3",method = RequestMethod.GET)
    public  List<TypeVO> c2c3(int row) {
        List<TypeVO> typeVOList=new ArrayList<>();
        List<Type> c2s=typeService.getTypes2(row);
        for (Type c2:c2s){
            TypeVO vo=new TypeVO();
            vo.setC2(c2);
            List<Type> c3s=typeService.getTypes3(row,c2.getId());
            vo.setC3(c3s);
            typeVOList.add(vo);
        }
        return typeVOList;
    }

    //type?name=&parent=1&info2=1&level=2&method=add
    //新增一个类型
    @RequestMapping(value = "addType",method = RequestMethod.GET)
    public ModelAndView add(Type type) {
        int res=typeService.add(type);
        MsgResult msg=new MsgResult();
        ModelAndView mav=new ModelAndView();
        msg.setCode(0);
        if (res==1){
            msg.setCode(1);
            msg.setMsg("类型添加成功");
            mav.addObject("msgresult",msg);
            mav.setViewName("res");
        }
        mav.addObject("msgresult",msg);
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "admin/typeC2C3",method = RequestMethod.GET)
    public  List<Type> findc1() {
        List<Type> c1types = typeService.getTypes1();
        return c1types;
    }
}
